<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      div {
        width: 100px;
        height: 100px;
        background-color: pink;

        position: absolute;
        top: 0px;
        left: 0px;
      }
    </style>
  </head>
  <body>
    <div id="box" style="left:0px;top:0px"></div>
    <script>
      var box = document.getElementById("box");

      var timer = null;
      // 点击一下 小盒子用1000毫秒 移动到(300,400)
      box.onclick = function () {
        // 计划用1000毫秒搞定 每帧40毫秒 合25帧
        var frames = 1000 / 40;

        /* 计算每帧(frame)偏移量 */
        var leftOffsetPerFrame = 300 / frames;
        var topOffsetPerFrame = 400 / frames;

        timer = setInterval(function () {
          box.style.left =
            parseFloat(box.style.left) + leftOffsetPerFrame + "px";
          box.style.top = parseFloat(box.style.top) + topOffsetPerFrame + "px";
          console.log("left/top", box.style.left, box.style.top);
        }, 40);

        /* 1000毫秒后拿掉timer */
        setTimeout(function () {
          clearInterval(timer);
        }, 1000);
      };
    </script>
  </body>
</html>
